﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using TpsAutoSurvey_PDA_Version_.Presenter;
using TpsAutoSurvey_PDA_Version_.RealizedClasses;

namespace TpsAutoSurvey_PDA_Version_.SmsModule
{
    class SmsCommand:CommandCenter
    {
        private MessageInterceptor interceptor;
        
        public SmsCommand(SurveyInterface c):base(c)
        {
            interceptor = new MessageInterceptor();
            interceptor.InterceptionAction = InterceptionAction.NotifyAndDelete;
            interceptor.MessageCondition.CaseSensitive = false;
            interceptor.MessageCondition.Property = Microsoft.WindowsMobile.PocketOutlook.MessageInterception.MessageProperty.Body;
            interceptor.MessageCondition.ComparisonType = Microsoft.WindowsMobile.PocketOutlook.MessageInterception.MessagePropertyComparisonType.StartsWith;
            interceptor.MessageCondition.ComparisonValue = "cmd"; 
            interceptor.MessageReceived += new MessageInterceptorEventHandler(interceptor_MessageReceived);
            
        }

        void interceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
            SmsMessage msg = (SmsMessage)e.Message;
            this.comingNumber = msg.From.Name;
            string str = msg.Body;

            if (str.Length < 3)
            { return; }
            if (str.Substring(0, 3) == "cmd")
            {
                OnMsgRecieved(str, msg.From.Name);
            }
        }
        public override void SendBackMsg(string str,string sender)
        {
            if (sender.Length == 0) return;
            try
            {
                SmsMessage msg = new SmsMessage(sender, str);
                msg.Send();
            }
            catch (Exception a)
            {
                ToolClass.Log(a.Message);
            }
        }
    }
}
